package com.dgw.controller;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgw.entity.Teacher;
import com.dgw.service.TeacherService;
import com.dgw.utils.Page;
import com.dgw.utils.PageRows;
import com.dgw.utils.Result;
import com.dgw.utils.ResultUtil;
import com.github.pagehelper.PageInfo;

@Controller
public class TeacherController {

	@Autowired
	private TeacherService teacherService;
	
	/**
	 * 跳转课程列表
	 * @return
	 */
	@RequestMapping(value = "/teacher", method = RequestMethod.GET)
	public String teacher(){
		return "teacher/teacher_list";
	}
	
	/**
	 *  查询课程列表
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/teacher/list", method = RequestMethod.GET)
	@ResponseBody
	public Result<PageInfo<Teacher>> teacherList(String keyword,PageRows pageRows) throws UnsupportedEncodingException{
		keyword= new String(keyword.getBytes("iso-8859-1"),"utf-8");
		PageInfo<Teacher> teachers = teacherService.selectByPageAndSelections(keyword, pageRows);
		return ResultUtil.success(teachers);
	}
	
	/**
	 *  查询课程列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/teacher/add", method = RequestMethod.GET)
	public String teacherAdd(){
		return "teacher/teacher_add";
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/teacher/add", method = RequestMethod.POST)
	@ResponseBody
	public Result teacherSave(Teacher teacher){
		int insert = teacherService.insert(teacher);
		if(insert>0){
			return ResultUtil.success();
		}
		return ResultUtil.error(444, "保存失败");
	}
	
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/teacher/edit", method = RequestMethod.GET)
	public String teacherEdit(String tId,Model model){
		Teacher teacher = teacherService.selectByPrimaryKey(tId);
		model.addAttribute("teacher", teacher);
		return "teacher/teacher_edit";
	}
	

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/teacher/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result teacherUpdate(Teacher teacher){
		int insert = teacherService.updateByPrimaryKeySelective(teacher);
		if(insert>0){
			return ResultUtil.success();
		}
		return ResultUtil.error(444, "修改失败");
	}
	
	
}
